﻿///todo: eval refactoring this into a more globally impacting name. IsDirty could just be sub functions of data binding operations
///todo: prototype this js into more of an object class pattern

var _ignoreIsPageDirty = true; //temporary. use until mass adoption of this pattern exists.
var _originalObject = {}; //the unchanged object, retreived from session unless provoked to do otherwise with the ExpireOriginalObject function
var _workingObject = {} //the object being worked in the current instance. this will help maintain changes while objects aren't on the screen.
var _dirtyConditions = []; //dirtyCondition: {jsonPropertyPath, sourceValue}

$(document).ready(function () {
    if ($.isEmptyObject(_originalObject) || !IsPageDirty()) {
        GetOriginalObject();
    }
});

function GetOriginalObject() {
    var isOriginalObjectExpired = $.cookies.get("isOriginalObjectExpired");
    if (isOriginalObjectExpired != null) {
        $.cookies.set("isOriginalObjectExpired", null); 
    }

    var originalObjectRequest = {
        "FunctionName": "GetOriginalObject",
        "isOriginalObjectExpired": isOriginalObjectExpired
    };

    var originalObjectResponseDelegate = function (jsonResponse) {
        _originalObject = jsonResponse;
    };

    var originalObjectRequestManager = new RequestManager(originalObjectRequest, originalObjectResponseDelegate, true);
    originalObjectRequestManager.ExecuteRequest();
}

///we may need the ability to kill what's in session to prevent false positive/negative IsDirty results
function ExpireOriginalObject() {
    $.cookies.set("isOriginalObjectExpired", true, { expires: new Date().getTime() + (20 * 60 * 1000) }); //expire in 20 mins
}

///iterates through all items in the _dirtyConditions array and validates them 
function IsPageDirty() {
    var result = _dirtyConditions.length > 0;

    return result;
}

///validates a given item value against the _originalObjects property value
function IsDirty(jsonPropertyPath, sourceValue) {
    if ($.isEmptyObject(_originalObject)) {
        return true;
    }

    var originalValue = JSONSelect.match(jsonPropertyPath, _originalObject); //todo: safety first, check for length = 0
    
    ///we don't have an object in memory to compare against
    if (originalValue.length == 0) {
        return true;
    }
     
    if (originalValue[0] == null && sourceValue != null)
        return sourceValue != "";

    return originalValue[0] != sourceValue;
}

function PutDirtyCondition(item) {
    var foundCondition = $(_dirtyConditions).filter(function () {
        return this.jsonPropertyPath == item.jsonPropertyPath;
    });

    if (IsDirty(item.jsonPropertyPath, item.sourceValue)){
        if (foundCondition.length > 0) {
            foundCondition[0].sourceValue = item.sourceValue;
        }
        else {
                _dirtyConditions.push(item);
        }
    }
    else if(foundCondition.length > 0){
        _dirtyConditions.splice($.inArray(foundCondition, _dirtyConditions), 1)        
    }
}

function PutDirtyConditions(items) {
    $(items).each(function () {
        PutDirtyCondition(this);
    });
}

function PurgeDirtyConditions() {
    _dirtyConditions = [];
}